// miniprogram/pages/me/me.js
var that
var app = getApp();
const db = wx.cloud.database();
const _ =db.command;
Page({
    //收藏和喜欢
       /**
   * 页面的初始数据
   */
  data: {
     flat:0,
     topics:{},
     length:0,
   },
     /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    that = this
    wx.cloud.init({
      env: 'test-5gwxfg0see31cde1',
    })
    that.data.flat = options.flat
    that.chooseFlat();
  },
  chooseFlat:function(){
    //flat==1时为收藏内容
    if(that.data.flat==1){
      db.collection('collect')
      .field({
        c_id : true
      })
      .get({
        success:function(res){
          var id = new Array(res.data.length)
          for(var i=0;i<res.data.length;i++){
            id[i] = res.data[i].c_id
          }
          console.log(id)
          db.collection('topic').where({
            _id : _.in(id)
          }).get({
            success : function(res){
              console.log(res)
              that.data.topics = res.data
              that.data.length=  res.data.length;
              that.setData({
                topics : that.data.topics,
                length : that.data.length,
              })
              if(that.data.topics.length==0){
                wx.showToast({
                  icon: 'none',
                  title: '你暂未收藏任何内容',
                })
                setTimeout(function() {
                  wx.navigateBack({
                    url: "../me/me"
                  })
                }, 1500)
              }
            }
          })
        }
      })
    }
    //点赞列表
    else if(that.data.flat == 2){
      db.collection('like')
      .field({
        l_id : true
      })
      .get({
        success:function(res){
          var id = new Array(res.data.length)
          for(var i=0;i<res.data.length;i++){
            id[i] = res.data[i].l_id
          }
          console.log(id)
          db.collection('topic').where({
            _id : _.in(id)
          }).get({
            success : function(res){
              console.log(res)
              that.data.topics = res.data
              that.data.length = res.data.length
              that.setData({
                topics : that.data.topics,
                length : that.data.length
              })
              if(that.data.topics.length==0){
                wx.showToast({
                  icon: 'none',
                  title: '你暂未点赞任何内容',
                })
                setTimeout(function() {
                  wx.navigateBack({
                    url: "../me/me"
                  })
                }, 1500)
              }
            }
          })
        }
      })
    }
    //历史纪律
    else if(that.data.flat==3){
        db.collection('topic').where({
          _openid : app.globalData.openid
        })
        .get({
          success : function(res){
            console.log(res.data)
            that.data.topics = res.data
            that.data.length=res.data.length
            that.setData({ 
              topics : that.data.topics,
              length : that.data.length
          })
          if(that.data.topics.length==0){
            wx.showToast({
              icon: 'none',
              title: '你暂未发布任何内容',
            })
            setTimeout(function() {
              wx.navigateBack({
                url: "../me/me"
              })
            }, 1500)
          }
        }
      })
    }
    //关注列表
    else if(that.data.flat==4){
      db.collection('follow')
      .field({
        f_id : true
      })
      .get({
        success:function(res){
          var id = new Array(res.data.length)
          for(var i=0;i<res.data.length;i++){
            id[i] = res.data[i].f_id
          }
          console.log(id)
          db.collection('topic').where({
            _openid : _.in(id)
          }).get({
            success : function(res){
              console.log(res)
              that.data.topics = res.data
              that.data.length=res.data.length
              that.setData({
                topics : that.data.topics,
                length : that.data.length
              })
              if(that.data.topics.length==0){
                wx.showToast({
                  icon: 'none',
                  title: '你暂未关注任何人',
                })
                setTimeout(function() {
                  wx.navigateBack({
                    url: "../me/me"
                  })
                }, 1500)
              }
            }
          })
        }
      })
    }
  },
})